﻿uniform sampler2D SourceMap;
uniform float MinLevel;
uniform float BlurRadius;
uniform float BloomScale;

#define SAMPLES 18
#define SAMPLES2 9.0

 vec3 blur(vec2 uv,float xi,float yi){

	vec3 r;

	for(int i=0;i<SAMPLES;i++){

		vec3 nv = texture2D( SourceMap,uv ).rgb;
		if(((nv.r+nv.g+nv.b)/3.0)>MinLevel){

			r+=nv;

		}
		uv.x+=xi;
		uv.y+=yi;
	}
	return r/(float)SAMPLES;

}

void main(){

	vec3 rgb;

	float a2 = texture2D( SourceMap,gl_TexCoord[0].st ).a;

	if(a2<0.001){

		discard;
		return;

	}

	vec2 uv = gl_TexCoord[0].st;

	int a;

	for(a=0;a<180;a+=35){

		float ang = a;
		ang*=0.0174532925;

		float xi = cos(ang)*BlurRadius;
		float yi = sin(ang)*BlurRadius;

		vec2 nv = uv;

		nv.x-=(xi*(SAMPLES2));
		nv.y-=(yi*(SAMPLES2));

		rgb+=blur( nv,xi,yi );

	}

	rgb/=BloomScale;

	rgb = texture2D( SourceMap, uv ).rgb * 0.6 + rgb * 0.6;
	

	gl_FragColor.rgb = rgb;
	gl_FragColor.a = a2;

}